package com.talking.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.talking.App;
import com.talking.databinding.ActivitySignBinding;
import com.talking.presenter.UserPresenter;
import com.talking.utils.Utils;
import com.talking.view.main.MainActivity;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class SignActivity extends AppCompatActivity {
    private ActivitySignBinding binding;
    private String account="",mail="",p1="",p2="";
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        binding= ActivitySignBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.signLogo.setTypeface(Typeface.createFromAsset(getAssets(),"FZSJ-QIYXRYY.TTF"));

        binding.signAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                account=charSequence.toString();
                check();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.signEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mail=charSequence.toString();
                check();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.signPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
p1=charSequence.toString();
check();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        binding.signPassword2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
p2=charSequence.toString();
check();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
    private void check() {
        binding.signSign.setEnabled(Utils.isEmail(mail) &&!p1.equals("")&p1.equals(p2)&&!account.equals("")&&Utils.passwordCorrect(p1));
    }
    public void sign(View view) {
        UserPresenter.sign(binding.signAccount.getText().toString(), binding.signEmail.getText().toString(), binding.signPassword.getText().toString(), binding.signPassword2.getText().toString(), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                SignActivity.this.runOnUiThread(() -> Utils.toast( e.toString()));
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.body().string().equals("创建成功")){
                    SignActivity.this.runOnUiThread(() -> Utils.toast("注册成功"));
                    @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
                    edit.putString("account",binding.signAccount.getText().toString());
                    edit.putString("password",binding.signPassword.getText().toString());
                    edit.putString("email",binding.signEmail.getText().toString());
                    edit.putBoolean("autoLogin",true);
                    edit.apply();
                    SignActivity.this.startActivity(new Intent(SignActivity.this, MainActivity.class));
                    SignActivity.this.finish();
                }
            }
        });
    }
}